<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<input type="text"id="msg">
<input type="button" value="send"  onclick="sendMessage()">
<body>
<script src="/res/jquery/jquery.js"></script>
<!--<script src="/res/text-encoding/encoding.js"></script>-->
<!--<script src="/res/tio_js/tiocookie.js"></script>-->
<script>
    var initCookies = document.cookie ? document.cookie.split("; ") : [];
    console.log("initcookie--->"+document.cookie);
    console.log("initcookie:",initCookies);
    var sessionName=initCookies[0].split("=")[0];
    var sessionValue=initCookies[0].split("=")[1];
    var  timer=null;
    console.log("sessionname:",sessionName);
    console.log("sessionValue", sessionValue);
    var url="wss://mpv2.csqiji.com:9325";
    var siteim_ws_protocol = 'wss'; // ws 或 wss
    var siteim_server = '127.0.0.13';
    var siteim_port = 93253;
    var type=location.search.split("?")[1].split("&")[0];
    console.log(type);
    var  id=type==="kf"?209:2800;
    let promise = new Promise(function(resolve, reject) {
        $.ajax({
            url:'/mytio/im/imserver.tio_x',
            data: {
                groupid: encodeURIComponent(location.pathname)
            },
            // async: false,
            // timeout: 1000,
            success: function(resp) {
                var data = resp.data;
                siteim_server = data.ip;
                siteim_port = data.port;
                console.log('imserver---------------->', data);
            }
        });
        var queryString = sessionName + '=' + encodeURIComponent(sessionValue);
        // if (tesft_tio_fdfdse) {
        //     queryString = queryString + '&' + tesoegEgac + '=' + encodeURIComponent(tesft_tio_fdfdse);
        // }
        //  url = siteim_ws_protocol + '://' + siteim_server + ':' + siteim_port + '?' + queryString;
        url=url+"?"+queryString;

        // if (reconn) {
        //     url += '&tiows_reconnect=true';
        // }
        console.log("url:"+url+"location name:"+location.pathname);
        resolve();
    });
    promise.then(function() {
        $.ajax("/mytio/login.tio_x",{//登录
            data:{
                loginname:id
            },async:false,
            success:function(res){
                console.log(res);
                setTimeout(function(){
                    // siteim.send(SiteImCommand.KF_JOIN_P2P_REQ,{
                    //     uid:init_data.mine.id
                    // })
                },1000)
            }
        })
    });



    var ws = new WebSocket(url);
    ws.onopen = function()
    {

        var  id=type==="kf"?209:2800;
        var req={uid:id};
        var commandstr="00033"+JSON.stringify(req);
        var uint8array_join=new TextEncoder().encode(commandstr);
        ws.send(uint8array_join.buffer);
        console.log("准备发送");
        var uint8array = new TextEncoder().encode("00001");


         timer = setInterval(function() {
             console.log("send heart");
             ws.send(uint8array.buffer);
        }, 3000);


        alert("发送心跳...");
    };

    ws.onmessage = function (evt)
    {
        console.log("onmessage:"+JSON.stringify(evt));
        var received_msg = evt.data;
        console.log(evt.data);
        var reader = new FileReader();
        reader.readAsText(evt.data);
        reader.onload=function(f){
            // var result=document.getElementById("result");
            //显示文件
            console.log("reader  message---->"+this.result);
        };

    };

    ws.onclose = function()
    {
        // 关闭 websocket
        alert("连接已关闭...");
        clearInterval(timer);
        console.log(url+"&tiows_reconnection=ture");
        ws=new WebSocket(url+"&tiows_reconnection=ture");
        ws.onopen=function(){
            alert("已重连")
        };
        ws.onmessage=function(evt){
            console.log("onmessage:"+JSON.stringify(evt));
            var received_msg = evt.data;
            console.log(evt.data);
            var reader = new FileReader();
            reader.readAsText(evt.data);
            reader.onload=function(f){
                // var result=document.getElementById("result");
                //显示文件
                console.log("reader  message---->"+this.result);
            };
        }
    };
function sendMessage(){
    setTimeout(function(){
        var msg=document.getElementById("msg").value;
        var  id=type==="kf"?2800:209;
        var req={toId:id,content:msg};
        var str="00029";
        var  commandstr=str+JSON.stringify(req);
        console.log("准备发送  。。。。"+commandstr);
        var uint8array = new TextEncoder().encode(commandstr);
        ws.send(uint8array.buffer);
    },2000)


}



</script>
</body>
</html>
